﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class FareStairs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("FA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                if (Request.QueryString["fare_code"] == null)
                {
                    lblError.Text = "Illegal page call!";
                }
                else
                {
                    lblFareCode.Text = Request.QueryString["fare_code"];
                    lblSegNo.Text = Request.QueryString["seg_sn"];
                    RefreshStairs();
                }
            }
            btnChange.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnApprove.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            RefreshStairs();
        }

        protected void RefreshStairs()
        {
            TableRow tabRow;
            TableCell tabCell;
            int height;

            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            DataTable dtStairs = new DataTable();
            cmd.CommandText = "bll_fare_get_stairs";
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
            para = cmd.Parameters.AddWithValue("@seg_sn", lblSegNo.Text);
            TinyDAL.Exec(ref cmd, ref dtStairs);

            tabRow = new TableRow();
            foreach (DataRow rowAdtPrice in dtStairs.Rows)
            {

                tabCell = new TableCell();
                height = Convert.ToInt16(rowAdtPrice["adt_price"]) / 20;
                if (height < 10)
                    height = 10;
                if (Convert.ToDecimal(rowAdtPrice["adt_price"]) < 0)
                    tabCell.Text =  Convert.ToSingle(rowAdtPrice["adt_price"]).ToString("F2") + "<br/><img src='images/bar_red.bmp' width=10 height=10><br/>" + rowAdtPrice["dbf"].ToString();
                else
                {
                    if (Convert.ToBoolean(rowAdtPrice["approved"]) && rowAdtPrice["action"].ToString() == "-")
                        tabCell.Text = Convert.ToSingle(rowAdtPrice["adt_price"]).ToString("F2") + "<br/><img src='images/bar_green.bmp' width=10 height=" + height.ToString() + "><br/>" + rowAdtPrice["dbf"].ToString();
                    else if (Convert.ToBoolean(rowAdtPrice["approved"]) && rowAdtPrice["action"].ToString() != "-")
                        tabCell.Text = Convert.ToSingle(rowAdtPrice["adt_price"]).ToString("F2") + "<br/><img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + "><br/>" + rowAdtPrice["dbf"].ToString();
                    else
                        tabCell.Text = Convert.ToSingle(rowAdtPrice["adt_price"]).ToString("F2") + "<br/><img src='images/bar_yellow.bmp' width=10 height=" + height.ToString() + "><br/>" + rowAdtPrice["dbf"].ToString();
                }
                tabCell.VerticalAlign = VerticalAlign.Bottom;
                tabCell.HorizontalAlign = HorizontalAlign.Center;
                tabCell.Width = 70;
                tabRow.Cells.Add(tabCell);
            }
            tabAdtPrice.Rows.Add(tabRow);
            
            tabRow = new TableRow();
            foreach (DataRow rowChdPrice in dtStairs.Rows)
            {

                tabCell = new TableCell();
                height = Convert.ToInt16(rowChdPrice["chd_price"]) / 20;
                if (height < 10)
                    height = 10;
                if (Convert.ToDecimal(rowChdPrice["chd_price"]) < 0)
                    tabCell.Text = Convert.ToSingle(rowChdPrice["chd_price"]).ToString("F2") + "<br/><img src='images/bar_red.bmp' width=10 height=10><br/>" + rowChdPrice["dbf"].ToString();
                else
                {
                    if (Convert.ToBoolean(rowChdPrice["approved"]) && rowChdPrice["action"].ToString() == "-")
                        tabCell.Text = Convert.ToSingle(rowChdPrice["chd_price"]).ToString("F2") + "<br/><img src='images/bar_green.bmp' width=10 height=" + height.ToString() + "><br/>" + rowChdPrice["dbf"].ToString();
                    else if (Convert.ToBoolean(rowChdPrice["approved"]) && rowChdPrice["action"].ToString() != "-")
                        tabCell.Text = Convert.ToSingle(rowChdPrice["chd_price"]).ToString("F2") + "<br/><img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + "><br/>" + rowChdPrice["dbf"].ToString();
                    else
                        tabCell.Text = Convert.ToSingle(rowChdPrice["chd_price"]).ToString("F2") + "<br/><img src='images/bar_yellow.bmp' width=10 height=" + height.ToString() + "><br/>" + rowChdPrice["dbf"].ToString();
                }
                tabCell.VerticalAlign = VerticalAlign.Bottom;
                tabCell.HorizontalAlign = HorizontalAlign.Center;
                tabCell.Width = 70;
                tabRow.Cells.Add(tabCell);
            }
            tabChdPrice.Rows.Add(tabRow);

            tabRow = new TableRow();
            foreach (DataRow rowInfPrice in dtStairs.Rows)
            {

                tabCell = new TableCell();
                if (Convert.ToDecimal(rowInfPrice["inf_price"]) < 0)
                    tabCell.Text = Convert.ToSingle(rowInfPrice["inf_price"]).ToString("F2") + "<br/><img src='images/bar_red.bmp' width=10 height=10><br/>" + rowInfPrice["dbf"].ToString();
                else
                {
                    height = Convert.ToInt16(rowInfPrice["inf_price"]) / 20;
                    if (height < 10)
                        height = 10;
                    if (Convert.ToBoolean(rowInfPrice["approved"]) && rowInfPrice["action"].ToString() == "-")
                        tabCell.Text = Convert.ToSingle(rowInfPrice["inf_price"]).ToString("F2") + "<br/><img src='images/bar_green.bmp' width=10 height=" + height.ToString() + "><br/>" + rowInfPrice["dbf"].ToString();
                    else if (Convert.ToBoolean(rowInfPrice["approved"]) && rowInfPrice["action"].ToString() != "-")
                        tabCell.Text = Convert.ToSingle(rowInfPrice["inf_price"]).ToString("F2") + "<br/><img src='images/bar_blue.bmp' width=10 height=" + height.ToString() + "><br/>" + rowInfPrice["dbf"].ToString();
                    else
                        tabCell.Text = Convert.ToSingle(rowInfPrice["inf_price"]).ToString("F2") + "<br/><img src='images/bar_yellow.bmp' width=10 height=" + height.ToString() + "><br/>" + rowInfPrice["dbf"].ToString();
                }
                tabCell.VerticalAlign = VerticalAlign.Bottom;
                tabCell.HorizontalAlign = HorizontalAlign.Center;
                tabCell.Width = 70;
                tabRow.Cells.Add(tabCell);
            }
            tabInfPrice.Rows.Add(tabRow);

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            manage_fare_stair("A");
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            manage_fare_stair("D");
        }
        protected void btnChange_Click(object sender, EventArgs e)
        {
            manage_fare_stair("C");
        }
        protected void manage_fare_stair(string action)
        {
            if (Session["ACL"].ToString().IndexOf("FA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code, dbf;
            try
            {
                dbf = Convert.ToInt16(txtDBF.Text);
            }
            catch (Exception ex)
            {
                lblError.Text = "Check 'Days before flight' format!";
                return;
            }
            if (dbf < 0 || dbf > 365)
            {
                lblError.Text = "Invalid 'Days before flight'!";
                return;
            }

            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            try
            {
                cmd.CommandText = "bll_fare_manage_stair";
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
                para = cmd.Parameters.AddWithValue("@seg_sn", lblSegNo.Text);
                para = cmd.Parameters.AddWithValue("@dbf", -1 * dbf);
                para = cmd.Parameters.AddWithValue("@action", action);
                para = cmd.Parameters.AddWithValue("@adt_price", txtAdtPrice.Text);
                para = cmd.Parameters.AddWithValue("@chd_price", txtChdPrice.Text);
                para = cmd.Parameters.AddWithValue("@inf_price", txtInfPrice.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Done.";
                        RefreshStairs();
                        break;
                    case 1:
                        lblError.Text = "Invalid action!";
                        break;
                    case 2:
                        lblError.Text = "Action failed!";
                        break;
                    case 3:
                        lblError.Text = "Please delete unapproved request first!";
                        break;
                    case 4:
                        lblError.Text = "Deleting unapproved request failed!";
                        break;
                    case 5:
                        lblError.Text = "Illegal action, check exist data!";
                        break;
                    case 6:
                        lblError.Text = "Illegal Days-before-flight value!";
                        break;
                    case 7:
                        lblError.Text = "Product not found!";
                        break;
                    case 8:
                        lblError.Text = "It's a OPEN product, DBF always should be 0!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            ApproveStair(1);
        }
        protected void btnReject_Click(object sender, EventArgs e)
        {
            ApproveStair(0);
        }
        protected void ApproveStair(int approve)
        {
            if (Session["ACL"].ToString().IndexOf("FA4") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            int ret_code, dbf;
            try
            {
                dbf = Convert.ToInt16(txtDBF.Text);
            }
            catch (Exception ex)
            {
                lblError.Text = "Check 'Days before flight' format!";
                return;
            }
            if (dbf < 0 || dbf > 365)
            {
                lblError.Text = "Invalid 'Days before flight'!";
                return;
            }
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            try
            {
                cmd.CommandText = "bll_fare_approve_stair";
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
                para = cmd.Parameters.AddWithValue("@seg_sn", lblSegNo.Text);
                para = cmd.Parameters.AddWithValue("@dbf", -1 * dbf);
                para = cmd.Parameters.AddWithValue("@approve", approve);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                switch (ret_code)
                {
                    case 0:
                        lblError.Text = "Done.";
                        RefreshStairs();
                        break;
                    case 1:
                        lblError.Text = "Request not found!";
                        break;
                    case 2:
                        lblError.Text = "Commit failed!";
                        break;
                    case 3:
                        lblError.Text = "Illegale action!";
                        break;
                    default:
                        lblError.Text = "Error:" + ret_code.ToString();
                        break;
                }
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }

        protected void btnRetreive_Click(object sender, EventArgs e)
        {
            int dbf;
            try
            {
                dbf = Convert.ToInt16(txtDBF.Text);
            }
            catch (Exception ex)
            {
                lblError.Text = "Check 'Days before flight' format!";
                return;
            }
            if (dbf < 0 || dbf > 365)
            {
                lblError.Text = "Invalid 'Days before flight'!";
                return;
            }
            SqlCommand cmd = new SqlCommand();
            SqlParameter para;
            DataTable dtStair = new DataTable();
            cmd.CommandText = "bll_fare_get_stair";
            para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
            para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
            para = cmd.Parameters.AddWithValue("@seg_sn", lblSegNo.Text);
            para = cmd.Parameters.AddWithValue("@dbf", -1 * dbf);
            TinyDAL.Exec(ref cmd, ref dtStair);
            if (dtStair.Rows.Count > 0)
            {
                txtAdtPrice.Text = Convert.ToSingle(dtStair.Rows[0]["adt_price"]).ToString();
                txtChdPrice.Text = Convert.ToSingle(dtStair.Rows[0]["chd_price"]).ToString();
                txtInfPrice.Text = Convert.ToSingle(dtStair.Rows[0]["inf_price"]).ToString();
            }
            else
                lblError.Text = "Not found!";
            RefreshStairs();
        }
    }
}